<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\website\Rooms;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Db;
use broadcast\redis\setRedis;
use app\admin\model\SystemWebsite;

/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="站点管理")
 */
class RoomConfig extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new Rooms();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
                

 
            $countModel = $this->model->where($where);
            if (session("admin.group") != 1) {
                $countModel->where('roomid','in',explode(",",session("admin.rooms")));
            }
            $count = $countModel->count();
            $listModel = $this->model->where($where);
            if (session("admin.group") != 1) {
                $listModel->where('roomid','in',explode(",",session("admin.rooms")));
            }

           
            $list = $listModel
            ->page($page, $limit)
                ->order($this->sort)
                ->select()->each(function ($item) {
                     //获取网站信息  start
                    $website = setRedis::getCache(session("website"),'info');
                    if (empty($website)) {
                        $website = SystemWebsite::where("website_ident",session("website"))->find();
                        if ($website) $website = $website->toArray();
                        setRedis::setCache(session("website"),"info",$website,0);
                    }
                    //获取网站信息  end


                    $item['status'] = (int)$item['status'];
                    if (strpos($website['reception_domain'],'?') !== false) { 
                        $item['roomslink'] = $website['reception_domain']."/".reversibleEncryption($website['website_ident'])."/".$item['roomid'];
                    }else{
                        $item['roomslink'] = $website['reception_domain']."/".reversibleEncryption($website['website_ident'])."/".$item['roomid'];
                    }
                    
                });
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {


        if ($this->request->isAjax()) {
            $post = $this->request->post();

            //设置值
            if(!isset($post['status'])) $post['status'] = '0';
            if(!isset($post['whether_robot'])) $post['whether_robot'] = '0';
            if(!isset($post['whether_force_login'])) $post['whether_force_login'] = '0';
            if(!isset($post['ispass'])) $post['ispass'] = '0';
            if(!isset($post['message_audit'])) $post['message_audit'] = '0';
            if(!isset($post['visitors_speak'])) $post['visitors_speak'] = '0';
            try {
                $save = $this->model->save($post);

                $roomInfo = Rooms::where("roomid",$post['roomid'])->find();
                $roomInfo = $roomInfo->toArray();
                setRedis::setCache(session("website"),"roomInfo".$post['roomid'],$roomInfo);

            } catch (\Exception $e) {
                $this->error('保存失败');
            }  
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }

        return $this->fetch();
    }

    
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        
        try {
            $roomId = $row['roomid'];
            $save = $row->delete();
            $roomInfo = Rooms::where("roomid",$roomId)->find();
            $roomInfo = $roomInfo->toArray();
            setRedis::setCache(session("website"),"roomInfo".$roomId,$roomInfo);
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    /**
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $post = $this->request->post();
        $row = $this->model->find($post['id']);

        empty($row) && $this->error('数据不存在');
        try {
            $save = $row->save([
                $post['field'] => $post['value'],
            ]);
            $roomInfo = Rooms::where("roomid",$row['roomid'])->find();
            $roomInfo = $roomInfo->toArray();
            setRedis::setCache(session("website"),"roomInfo".$row['roomid'],$roomInfo);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        
        empty($row) && $this->error('数据不存在');
        
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            
            //设置值
            if(!isset($post['status'])) $post['status'] = '0';
            if(!isset($post['whether_robot'])) $post['whether_robot'] = '0';
            if(!isset($post['whether_force_login'])) $post['whether_force_login'] = '0';
            if(!isset($post['ispass'])) $post['ispass'] = '0';
            if(!isset($post['message_audit'])) $post['message_audit'] = '0';
            if(!isset($post['visitors_speak'])) $post['visitors_speak'] = '0';
            try {
                $save = $row->save($post);
                $roomInfo = Rooms::where("roomid",$row['roomid'])->find();
                $roomInfo = $roomInfo->toArray();
                setRedis::setCache(session("website"),"roomInfo".$row['roomid'],$roomInfo);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        
        $this->assign([
            'id'          => $id,
            'row'         => $row,
        ]);
        return $this->fetch();
    }




}
